package com.wangx1n.boobey.service;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.wangx1n.boobey.entity.words.Example;
import com.wangx1n.boobey.entity.words.Phonetic;
import com.wangx1n.boobey.entity.words.Speech;
import com.wangx1n.boobey.entity.words.Word;
import com.wangx1n.boobey.util.ApiResult;
import com.wangx1n.boobey.util.CodeMsg;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangxin
 * @create 2020-06-12 17:12
 */
@Service
public class WordService {
    public  String getData(String word){
        Word word1 = new Word();
        Phonetic phonetic = new Phonetic();
        Speech speech = new Speech();
        List<Example> examples = new ArrayList<Example>();
        //http请求
        RestTemplate restTemplate = new RestTemplate();
        //gson处理json
        Gson gson = new Gson();
        //得到单词数据
        String  w =  restTemplate.getForObject("https://api.shanbay.com/bdc/search/?word="+word,String.class);
        //转换为jsonObject类型
        JsonObject wordResult = gson.fromJson(w, JsonObject.class);
        //判断单词是否没有
        if(! wordResult.get("status_code").toString().equals("0") ){
            //没有这个单词
            if(wordResult.get("status_code").getAsInt()==1){
                return gson.toJson(ApiResult.error(CodeMsg.NO_WORD));
            }
            //查询错误
            return gson.toJson(ApiResult.error(CodeMsg.ERROR_WORD));
        }
        //单词例句id
        String conentId = wordResult.get("data").getAsJsonObject().get("conent_id").getAsString();

        //得到例句数据
        String  exampleRes =  restTemplate.getForObject("https://api.shanbay.com/bdc/example/?vocabulary_id="+conentId,String.class);
        JsonObject exampleResult = gson.fromJson(exampleRes, JsonObject.class);
        //例句为array数组
        JsonArray exampleArray = exampleResult.get("data").getAsJsonArray();

        for (int i = 0; i < exampleArray.size(); i++) {
            Example example = new Example();
            example.setId(exampleArray.get(i).getAsJsonObject().get("id").getAsInt());
            example.setEn(exampleArray.get(i).getAsJsonObject().get("annotation").getAsString());
            example.setTranslation(exampleArray.get(i).getAsJsonObject().get("translation").getAsString());
            examples.add(example);
        }
        word1.setExamples(examples);
        word1.setWid(Integer.valueOf(conentId));
        word1.setWord(word);
        //设置音标
        phonetic.setUk(wordResult.get("data").getAsJsonObject().get("pronunciations").getAsJsonObject().get("uk").getAsString());
        phonetic.setUs(wordResult.get("data").getAsJsonObject().get("pronunciations").getAsJsonObject().get("us").getAsString());
        word1.setPhonetic(phonetic);
        //设置发音连接
        speech.setUk(wordResult.get("data").getAsJsonObject().get("audio_addresses").getAsJsonObject().get("uk").getAsJsonArray().get(0).getAsString());
        speech.setUs(wordResult.get("data").getAsJsonObject().get("audio_addresses").getAsJsonObject().get("us").getAsJsonArray().get(0).getAsString());
        word1.setSpeech(speech);

        return gson.toJson(ApiResult.success(word1,0));
    }
}
